﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MADSite
{
  public partial class Tickets3 : System.Web.UI.Page
  {
    private string resultTicket;
    private int id = -1;
    TrainManagerClient trainManagerClient = new TrainManagerClient();
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!Request.Cookies.AllKeys.Contains("id"))
      {
        id = trainManagerClient.InitUser();
      }
      else
      {
        id = Int32.Parse(Request.Cookies["id"].Value);
      }
      HttpCookie cookie = new HttpCookie("id", id.ToString());
      Response.Cookies.Add(cookie);
      placeTextBox.Text = Request.Cookies["ticket"].Value;
    }

    /// <summary>
    /// Вернуться к выбору рейса
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void prevButton_Click(object sender, EventArgs e)
    {
      Response.Redirect("Tickets2.aspx");
    }

    /// <summary>
    /// Перейти к оформлению
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void nextButton_Click(object sender, EventArgs e)
    {
      // Куки итогового билета
      HttpCookie resCookie = new HttpCookie("res");
      // TEMPORARY fix: local variables cannot be used for storing infformation
      //on client. 
      resCookie.Value = addTextBox.Text;
      //resCookie.Value = resultTicket;
      resCookie.Expires = DateTime.Now.AddDays(1);
      Response.Cookies.Add(resCookie);
      Response.Redirect("TicketsFin.aspx");
    }

    /// <summary>
    /// Добавить место из доступных
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void addButton_Click(object sender, EventArgs e)
    {
      try
      {
        placesLabel.Text += addTextBox.Text;
        resultTicket = addTextBox.Text;
        addButton.Enabled = false;
        clearButton.Enabled = true;
      }
      catch
      {

      }
    }

    /// <summary>
    /// Очистить список билетов
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void clearButton_Click(object sender, EventArgs e)
    {
      placesLabel.Text = "Выбранные места: ";
      resultTicket = string.Empty;
      addButton.Enabled = true;
      clearButton.Enabled = false;
    }
  }
}